﻿namespace CKFinder.Connector.CommandHandlers
{
    using CKFinder.Connector;
    using System;
    using System.Security;
    using System.Text.RegularExpressions;
    using System.Web;

    internal abstract class CommandHandlerBase
    {
        private CKFinder.Connector.Connector _Connector;
        private FolderHandler _CurrentFolder = FolderHandler.GetCurrent();
        private HttpRequest _Request = HttpContext.Current.Request;

        protected void CheckConnector()
        {
            if (!Config.Current.CheckAuthentication())
            {
                ConnectorException.Throw(500);
            }
        }

        protected void CheckRequest()
        {
            if (Regex.IsMatch(this.CurrentFolder.ClientPath, "(/\\.)|(\\.\\.)|(//)|([\\\\:\\*\\?\"\\<\\>\\|])"))
            {
                ConnectorException.Throw(0x66);
            }
            foreach (string str in this.CurrentFolder.ClientPath.Split(new char[] { '/' }))
            {
                if (Config.Current.CheckIsHiddenFolder(str))
                {
                    ConnectorException.Throw(0x6d);
                }
            }
            if (this.CurrentFolder.ResourceTypeInfo == null)
            {
                ConnectorException.Throw(12);
            }
            if (!this.CurrentFolder.FolderInfo.Exists)
            {
                if (this.CurrentFolder.ClientPath == "/")
                {
                    try
                    {
                        this.CurrentFolder.FolderInfo.Create();
                    }
                    catch (UnauthorizedAccessException)
                    {
                        ConnectorException.Throw(0x68);
                    }
                    catch (SecurityException)
                    {
                        ConnectorException.Throw(0x68);
                    }
                    catch
                    {
                        ConnectorException.Throw(110);
                    }
                }
                else
                {
                    ConnectorException.Throw(0x74);
                }
            }
        }

        public abstract void SendResponse(HttpResponse response);

        protected CKFinder.Connector.Connector Connector
        {
            get
            {
                if (this._Connector == null)
                {
                    this._Connector = (CKFinder.Connector.Connector) HttpContext.Current.Handler;
                }
                return this._Connector;
            }
        }

        public FolderHandler CurrentFolder
        {
            get
            {
                if (this._CurrentFolder == null)
                {
                    this._CurrentFolder = FolderHandler.GetCurrent();
                }
                return this._CurrentFolder;
            }
        }

        protected HttpRequest Request
        {
            get
            {
                return this._Request;
            }
        }
    }
}

